package cn.community.care.module.system.controller.admin.materialsinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.CommonResult;
import cn.community.care.framework.common.util.object.BeanUtils;
import static cn.community.care.framework.common.pojo.CommonResult.success;

import cn.community.care.framework.excel.core.util.ExcelUtils;

import cn.community.care.framework.operatelog.core.annotations.OperateLog;
import static cn.community.care.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.community.care.module.system.controller.admin.materialsinfo.vo.*;
import cn.community.care.module.system.dal.dataobject.materialsinfo.MaterialsinfoDO;
import cn.community.care.module.system.service.materialsinfo.MaterialsinfoService;

@Tag(name = "管理后台 - 物资基本信息")
@RestController
@RequestMapping("/system/materialsinfo")
@Validated
public class MaterialsinfoController {

    @Resource
    private MaterialsinfoService materialsinfoService;

    @PostMapping("/create")
    @Operation(summary = "创建物资基本信息")
    public CommonResult<Long> createMaterialsinfo(@Valid @RequestBody MaterialsinfoSaveReqVO createReqVO) {
        return success(materialsinfoService.createMaterialsinfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物资基本信息")
    public CommonResult<Boolean> updateMaterialsinfo(@Valid @RequestBody MaterialsinfoSaveReqVO updateReqVO) {
        materialsinfoService.updateMaterialsinfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物资基本信息")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteMaterialsinfo(@RequestParam("id") Long id) {
        materialsinfoService.deleteMaterialsinfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物资基本信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<MaterialsinfoRespVO> getMaterialsinfo(@RequestParam("id") Long id) {
        MaterialsinfoDO materialsinfo = materialsinfoService.getMaterialsinfo(id);
        return success(BeanUtils.toBean(materialsinfo, MaterialsinfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物资基本信息分页")
    public CommonResult<PageResult<MaterialsinfoRespVO>> getMaterialsinfoPage(@Valid MaterialsinfoPageReqVO pageReqVO) {
        PageResult<MaterialsinfoDO> pageResult = materialsinfoService.getMaterialsinfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialsinfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物资基本信息 Excel")
    @OperateLog(type = EXPORT)
    public void exportMaterialsinfoExcel(@Valid MaterialsinfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialsinfoDO> list = materialsinfoService.getMaterialsinfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物资基本信息.xls", "数据", MaterialsinfoRespVO.class,
                        BeanUtils.toBean(list, MaterialsinfoRespVO.class));
    }

}